package cn.skyquant.quant4j.api.goods;

import cn.skyquant.quant4j.api.dto.ResultDTO;
import cn.skyquant.quant4j.api.enums.BorkerType;
import cn.skyquant.quant4j.api.enums.GoodsType;
import cn.skyquant.quant4j.api.service.BaseServiceProxy;
import com.alibaba.fastjson.JSONObject;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GoodsServiceProxy extends BaseServiceProxy implements GoodsService{
    private static final String goods_url = default_urlfix+"/goods";

    public GoodsServiceProxy(String accountCode, BorkerType borkerType) {
        super(accountCode, borkerType);
    }


    @Override
    public ResultDTO<GoodsDTO> get(String symbol, BorkerType borkerType) {
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("symbol",symbol);
        paramMap.put("borkerType",borkerType.name());
        JSONObject jsonObject = get(goods_url,paramMap);
        if(jsonObject!=null){
            boolean ok = jsonObject.getBoolean("ok");
            String message = jsonObject.getString("message");
            if(ok){
                JSONObject data = jsonObject.getJSONObject("data");
                GoodsDTO dto = convertJsonToSymbol(data);
                return ResultDTO.success(dto);
            }else {
                return ResultDTO.error(message);
            }
        }
        return error;
    }

    @Override
    public ResultDTO<List<GoodsDTO>> findAllGoods(BorkerType borkerType) {
        return error;
    }

    @Override
    public ResultDTO<Void> saveGoods(String symbol, BorkerType borkerType, GoodsType type) {
        return error;
    }

    @Override
    public ResultDTO<Void> updateGoods(Integer id, GoodsType type, double avgSpread) {
        return error;
    }

    @Override
    public ResultDTO<Void> deleteGoods(Integer id) {
        return error;
    }

    @Override
    public ResultDTO<List<GoodsTickDTO>> findGoodsTickByGoodsId(Integer goodsId) {
        return error;
    }

    @Override
    public ResultDTO<Void> addGoodsTick(Integer goodsId, GoodsTickDTO goodsTickDTO) {
        return error;
    }

    @Override
    public ResultDTO<Void> deleteGoodsTickById(Integer id) {
        return error;
    }

    private static final GoodsDTO convertJsonToSymbol(JSONObject jsonObject) {
        GoodsDTO dto = new GoodsDTO();
        dto.symbol = jsonObject.getString("symbol");
        dto.avgSpread = jsonObject.getDouble("avgSpread");
        dto.borkerType = BorkerType.valueOf(jsonObject.getString("borkerType"));
        return dto;
    }
}
